package com.qingke.notlonely.app;

import com.qingke.notlonely.command.Command;
import com.qingke.notlonely.command.factory.CommandFactory;
import com.qingke.notlonely.command.factory.CommandFactory.CommandCode;
import com.qingke.notlonely.util.NLConsole;

public class NotLonelyApplication {
	private static NotLonelyApplication instance=null;
	private boolean isRun=true;
	private NotLonelyApplication(){
		
	}
	
	public static NotLonelyApplication getInstance(){
		if(instance==null){
			instance=new NotLonelyApplication();
		}
		return instance;
	}


	
	public void setRun(boolean isRun) {
		this.isRun = isRun;
	}

	public void run(){
		NLConsole.println("Welcome");
		String cmd;
		while(isRun){
			cmd=NLConsole.prompt("cmd>").trim().replaceAll(" ","").toUpperCase();
			
			CommandCode cmdCode = null;
			try{
				cmdCode=CommandCode.valueOf(cmd);
			}catch(Exception e){
				NLConsole.println("err input,you can input 'help' to look command.");
				
				continue;
			}
			
			CommandFactory factory = CommandFactory.getFactory(); 
			Command command = factory.buildCommand(cmdCode);
			if(command!=null){
				command.execute();
				continue;
			}
//			NLConsole.println("err input,you can input 'help' to look command.");
			
		}
	}
}
